﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Microsoft.AspNet.Identity.EntityFramework;

namespace RechercheEmplois.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public virtual Utilisateur Utilisateur { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<Utilisateur> Utilisateurs { get; set; }
        public DbSet<GroupUtilisateur> GroupUtilisateurs { get; set; }
        public DbSet<Domaine> Domaines { get; set; }
        public DbSet<MotCle> MotCles { get; set; }
        public DbSet<Offre> Offres { get; set; }
        public DbSet<Postule> Postules { get; set; }
        public DbSet<Sauvegarde> Sauvegardes { get; set; }
        public DbSet<Provine> Provines { get; set; }
        public DbSet<Ville> Villes { get; set; } 

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            // Change the name of the table to be Users instead of AspNetUsers
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users");
            modelBuilder.Entity<ApplicationUser>()
                .ToTable("Users");

            //Create table that respresent the relationship between table Offre and MotCle
            modelBuilder.Entity<Offre>()
                 .HasMany(c => c.MotCles).WithMany(i => i.Offres)
                 .Map(t => t.MapLeftKey("OffreID")
                     .MapRightKey("MotCleID")
                     .ToTable("OffreMotcle"));

            //modelBuilder.Entity<Postule>()
            //    .HasRequired(f => f.Recruiteur)
            //    .WithRequiredDependent()
            //    .WillCascadeOnDelete(false);

            //modelBuilder.Entity<Sauvegarde>()
            //   .HasRequired(f => f.Utilisateur)
            //   .WithRequiredDependent()
            //   .WillCascadeOnDelete(false);
        }
    }
}